package cn.parus.auth.controller;

import cn.parus.auth.POJO.OpaRequest;
import cn.parus.auth.service.OpaMGService;
import cn.parus.commons.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

/**
 * @Author: Axel
 * @Description: TODO OPA服务器管理策略
 **/
@Slf4j
@RestController
@RequestMapping("/api-opa")
public class OpaMGController {

    @Autowired
    OpaMGService opaMGService;

    /**
     * 获取OPA服务器的状态
     * @return
     */
    @Deprecated
    @GetMapping("/health")
    public Mono<Result> opaHealth() {
        return opaMGService.opaHealth()
                .map(data -> Result.success(data))
                .onErrorResume(e -> Mono.just(Result.error("系统繁忙，请稍后重试")));
    }

    /**
     * 获取到OPA中所有的策略
     * @return
     */
    @Deprecated
    @PostMapping("/getPolicies")
    public Mono<Result> getAllPolicies(@RequestBody OpaRequest opaRequest) {
        return opaMGService.getAllPolicy(opaRequest)
                .map(data -> Result.success(data))
                .onErrorResume(e -> Mono.just(Result.error("系统繁忙，请稍后重试")));
    }

    /**
     * 仅仅需要策略查询，并且返回查询的结果即可
     * @param opaRequest
     * @return
     */
    @Deprecated
    @PostMapping("/checkPermission")
    public Mono<Result> checkPermission(@RequestBody OpaRequest opaRequest) {
        return opaMGService.checkPermission(opaRequest)
                .map(data -> Result.success(data))
                .onErrorResume(e -> Mono.just(Result.error("系统繁忙，请稍后重试")));
    }
}
